#include "../src/reactor.hpp"
#include <fstream>

int main()
{
    //通过 dd if=/dev/zero of=./hello.txt bs=1G count=1 创建一个1G大小的文件
    //然后通过 echo "leihou" >> ./hello.txt 向文件末尾写入一串字符串
    std::ifstream ifs("./hello.txt", std::ios::in|std::ios::binary);
    if (ifs.is_open() == false) {
        printf("./hello.txt open file failed!\n");
        return false;
    } 
    size_t fsize = 0;
    std::string body;
    ifs.seekg(0, std::ios::end);
    fsize = ifs.tellg();
    ifs.seekg(0, std::ios::beg);
    body.resize(fsize);
    ifs.read(&body[0], fsize);
    if (ifs.good() == false) {
        ERR_LOG("read file content failed!");
        ifs.close();
        return false;
    }   
    ifs.close();
    
    bit_net::Socket sock;
    sock.create_client("127.0.0.1", 8500);
    std::string head = "PUT /1234.txt HTTP/1.1\r\n";
    head += "Connection: close\r\n";
    head += "Content-Length: " + std::to_string(body.size()) + "\r\n";
    head += "\r\n";
    int ret;
    ret = sock.send(head.c_str(), head.size());
    if (ret < 0) {
        ERR_LOG("send head error\n");
        return -1;
    }
    ret = sock.send(body.c_str(), body.size());
    if (ret < 0) {
        ERR_LOG("send body error\n");
        return -1;
    }
    char rsp[4096] = {0};
    ret = sock.recv(rsp, 4096);
    std::cout << rsp << std::endl;
    sock.close();
    return 0;
}
